// JavaScript Document

var isIE = (document.all) ? true : false;

var isIE6 = isIE && ([/MSIE (\d)\.0/i.exec(navigator.userAgent)][0][1] == 6);

var Class = {
	create: function() {
		return function() { this.initialize.apply(this, arguments); }
	}
}

var Extend = function(destination, source) {
	for (var property in source) {
		destination[property] = source[property];
	}
}

var Bind = function(object, fun) {
	return function() {
		return fun.apply(object, arguments);
	}
}

var Each = function(list, fun){
	for (var i = 0, len = list.length; i < len; i++) { fun(list[i], i); }
};

var Contains = function(a, b){
	return a.contains ? a != b && a.contains(b) : !!(a.compareDocumentPosition(b) & 16);
}


var OverLay = Class.create();
OverLay.prototype = {
  initialize: function(options) {

	this.SetOptions(options);
	
	this.Lay = document.getElementById(this.options.Lay) || document.body.insertBefore(document.createElement("div"), document.body.childNodes[0]);
	
	this.Color = this.options.Color;
	this.Opacity = parseInt(this.options.Opacity);
	this.zIndex = parseInt(this.options.zIndex);
	
	with(this.Lay.style){ display = "none"; zIndex = this.zIndex; left = top = 0; position = "fixed"; width = height = "100%"; }
	
	if(isIE6){
		this.Lay.style.position = "absolute";
		//ie6设置覆盖层大小程序
		this._resize = Bind(this, function(){
			this.Lay.style.width = Math.max(document.documentElement.scrollWidth, document.documentElement.clientWidth) + "px";
			this.Lay.style.height = Math.max(document.documentElement.scrollHeight, document.documentElement.clientHeight) + "px";
		});
		//遮盖select
		this.Lay.innerHTML = '<iframe style="position:absolute;top:0;left:0;width:100%;height:100%;filter:alpha(opacity=0);"></iframe>'
	}
  },
  //设置默认属性
  SetOptions: function(options) {
    this.options = {//默认值
		Lay:		null,//覆盖层对象
		Color:		"#000",//背景色
		Opacity:	20,//透明度(0-100)
		zIndex:		9998//层叠顺序
    };
    Extend(this.options, options || {});
  },
  //显示
  Show: function() {
	//兼容ie6
	if(isIE6){ this._resize(); window.attachEvent("onresize", this._resize); }
	//设置样式
	with(this.Lay.style){
		//设置透明度
		isIE ? filter = "alpha(opacity:" + this.Opacity + ")" : opacity = this.Opacity / 100;
		backgroundColor = this.Color; display = "block";
	}
  },
  //关闭
  Close: function() {
	this.Lay.style.display = "none";
	if(isIE6){ window.detachEvent("onresize", this._resize); }
  }
};



var LightBox = Class.create();
LightBox.prototype = {
  initialize: function(box, options) {
	
	this.Box = document.getElementById(box);//显示层
	
	this.OverLay = new OverLay(options);//覆盖层
	
	this.SetOptions(options);
	
	this.Fixed = !!this.options.Fixed;
	this.Over = !!this.options.Over;
	this.Center = !!this.options.Center;
	this.onShow = this.options.onShow;
	this.Speed = this.options.Speed;
	
	this.Box.style.zIndex = this.OverLay.zIndex + 1;
	this.Box.style.display = "none";
	
	//兼容ie6用的属性
	if(isIE6){
		this._top = this._left = 0; this._select = [];
		this._fixed = Bind(this, function(){ this.Center ? this.SetCenter() : this.SetFixed(); });
	}
  },
  //设置默认属性
  SetOptions: function(options) {
    this.options = {//默认值
		Over:	true,//是否显示覆盖层
		Fixed:	false,//是否固定定位
		Center:	true,//是否居中
		Speed: "fast",//淡出淡入的速度
		onShow:	function(){}//显示时执行
	};
    Extend(this.options, options || {});
  },
  //兼容ie6的固定定位程序
  SetFixed: function(){
	this.Box.style.top = document.documentElement.scrollTop - this._top + this.Box.offsetTop + "px";
	this.Box.style.left = document.documentElement.scrollLeft - this._left + this.Box.offsetLeft + "px";
	
	this._top = document.documentElement.scrollTop; this._left = document.documentElement.scrollLeft;
  },
  //兼容ie6的居中定位程序
  SetCenter: function(){
	this.Box.style.marginTop = document.documentElement.scrollTop - this.Box.offsetHeight / 2 + "px";
	this.Box.style.marginLeft = document.documentElement.scrollLeft - this.Box.offsetWidth / 2 + "px";
  },
  //显示
  Show: function(options) {
	//固定定位
	this.Box.style.position = this.Fixed && !isIE6 ? "fixed" : "absolute";

	//覆盖层
	this.Over && this.OverLay.Show();
		
	//$(this.Box).fadeIn(this.Speed);
	this.Box.style.display = "block";
	
	//居中
	if(this.Center){
		this.Box.style.top = this.Box.style.left = "50%";
		//设置margin
		if(this.Fixed){
			this.Box.style.marginTop = - this.Box.offsetHeight / 2 + "px";
			this.Box.style.marginLeft = - this.Box.offsetWidth / 2 + "px";
		}else{
			this.SetCenter();
		}
	}
	
	//兼容ie6
	if(isIE6){
		if(!this.Over){
			//没有覆盖层ie6需要把不在Box上的select隐藏
			this._select.length = 0;
			Each(document.getElementsByTagName("select"), Bind(this, function(o){
				if(!Contains(this.Box, o)){ o.style.visibility = "hidden"; this._select.push(o); }
			}))
		}
		//设置显示位置
		this.Center ? this.SetCenter() : this.Fixed && this.SetFixed();
		//设置定位
		this.Fixed && window.attachEvent("onscroll", this._fixed);
	}
	
	this.onShow();
  },
  //关闭
  Close: function() {
	this.Box.style.display = "none";
	this.OverLay.Close();
	if(isIE6){
		window.detachEvent("onscroll", this._fixed);
		Each(this._select, function(o){ o.style.visibility = "visible"; });
	}
  }
}; 

var box = null;

function OpenDialogBox(url, title, width, callback)
{ 
	if (box == null)
	{
		$.get(url,{rnd:rndwords(10)},
					function(data) 
					{
						CreateDialogBoxLayer(width, title, data, 1);
						$("#idBoxClose").bind("click",CloseBox);
						$("#idBoxCancel").bind("click",CloseBox);
						if (callback) callback();
						box = new LightBox("dialog-layer");
						box.Show();
					}, 
					function(){alert('数据加载失败');}
		); 
	}
}


function OpenDialogBoxByText(text, title, width, callback, options)
{
    if (box == null)
	{
			CreateDialogBoxLayer(width, title, text, 1);
			$("#idBoxClose").bind("click",CloseBox);
			$("#idBoxCancel").bind("click",CloseBox);
			if (callback) callback();
			box = new LightBox("dialog-layer",options);
			box.Show();			
	}
}

function OpenAlertBox(url, title, width, callback)
{ 
	if (box == null)
	{
		$.get(url,{rnd:rndwords(10)},
					function(data) 
					{
						CreateDialogBoxLayer(width, title, data, 2);
						$("#idBoxClose").bind("click",CloseBox);
						$("#idBoxConfirm").bind("click",CloseBox);
						if (callback) callback();
						box = new LightBox("dialog-layer");
						box.Show();
					}, 
					function(){alert('数据加载失败');}
		); 
	}
}

function OpenAlertBoxByText(text, title, width, callback)
{ 
	if (box == null)
	{
			CreateDialogBoxLayer(width, title, text, 2);
			$("#idBoxClose").bind("click",CloseBox);
			$("#idBoxConfirm").bind("click",CloseBox);
			if (callback) callback();
			box = new LightBox("dialog-layer");
			box.Show();			
	}
}

function OpenTipsBox(url, title, width, callback)
{ 
	if (box == null)
	{
		$.get(url,{rnd:rndwords(10)},
					function(data) 
					{
						CreateDialogBoxLayer(width, title, data, 0);
						$("#idBoxClose").bind("click",CloseBox);
						if (callback) callback();
						box = new LightBox("dialog-layer");
						box.Show();
					}, 
					function(){alert('数据加载失败');}
		); 
	}
}

function OpenTipsBoxByText(text, title, width, callback)
{
    if (box == null)
	{
			CreateDialogBoxLayer(width, title, text, 0);
			$("#idBoxClose").bind("click",CloseBox);
			if (callback) callback();
			box = new LightBox("dialog-layer");
			box.Show();			
	}
}

function CloseBox()
{
	if (box != null)
	{
		box.Close();
		$("#dialog-layer").remove();
		box = null;

	}
}
		
// create div of the dialog
function CreateDialogBoxLayer(width, title, data, dType)
{
    var layer =  $('<div id="dialog-layer" style="display:none"></div>');
	var content = '\
	<table width="' + width + '" border="0" cellpadding="0" cellspacing="0"><tr>\
	<td><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr>\
    <td class="msgbox_curve"><img src="/content/images/dialogbox/msgbox_lt.gif" width="5" height="8" /></td>\
    <td class="msgbox_shadow2"><img src="/content/images/dialogbox/blank.gif" width="1" height="1" /></td>\
    <td class="msgbox_curve"><img src="/content/images/dialogbox/msgbox_rt.gif" width="5" height="8" /></td>\
    </tr></table></td></tr><tr><td><table width="100%" border="0" cellpadding="0" cellspacing="0">\
    <tr><td class="msgbox_shadow"></td><td><div class="msgbox">\
    <div class="title"><span class="l">' + title + '</span><span class="close" id="idBoxClose"><a href="javascript:void(0);"></a></span></div>\
    <div class="mid">' + data;
    
	if (dType != 0)
	{
	    var button = '<p class="pix"></p><p style="padding-top:10px;text-align:right;">';
	    button += '<input type="button" id="idBoxConfirm" class="botton" value="确定" onMouseOut="this.className=\'botton\';" onMouseOver="this.className=\'botton2\';" />';
	    if (dType == 1)
	    {
	      button += '&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" id="idBoxCancel" class="botton_close1" value="取消" onMouseOut="this.className=\'botton_close1\';" onMouseOver="this.className=\'botton_close2\';" />';
	    }
	    button += '</p>';
	   content += button;
	}

	content += '\
	</div></div></td><td class="msgbox_shadow"></td></tr></table></td></tr><tr>\
    <td><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr>\
    <td class="msgbox_curve"><img src="/content/images/dialogbox/msgbox_lb.gif" width="5" height="8" /></td>\
    <td class="msgbox_shadow2"><img src="/content/images/dialogbox/blank.gif" width="1" height="1" /></td>\
    <td class="msgbox_curve"><img src="/content/images/dialogbox/msgbox_rb.gif" width="5" height="8" /></td>\
    </tr></table></td></tr></table>';
	
	var d=$(document.body);
	d.append(layer.html(content));
}

/* 随机 */
var WordsArr="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var rndword=function(){return WordsArr.substr(rndnum(0,51),1);}
var rndwords=function(len){var rtn=[];for(i=0;i<len;i++){rtn.push(rndword());}return rtn.join("");}
var rndnum=function(minnum,maxnum){return Math.ceil(Math.random()*(maxnum-minnum)+minnum);}